package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/order")
@RestController
public class OrderController {
    
    @Autowired
    private OrderService orderService;

    /**
     * 查看最近订单
     */
    @GetMapping("/userPage")
    public R findList(PageParam param){
        try {
            Page<OrdersDto> page=orderService.findList(param);

            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询失败");
        }
    }
    /**
     * 提交订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R add(@RequestBody Orders orders , HttpSession session){
        
        //1. 调用service
        User user = (User) session.getAttribute("user");
        int row = orderService.add(orders ,  user);
        
        //2. 判定
        if(row >0 ){
            return R.success("提交订单成功！");
        }else{
            return R.error("提交订单失败！");
        }
    }
}
